package com.kznnc.data_structure.current;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description
 * @Author houchenkai
 * @Created Date: 2021/7/6 10:36
 * @ClassName
 */
@SuppressWarnings("all")
public class ThreadPoolExecutorDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //创建线程的三种方式
        //runnableTask();
       // ReentrantReadWriteLock


        ThreadPoolExecutor.AbortPolicy rejectHandler = new ThreadPoolExecutor.AbortPolicy();
        LinkedBlockingQueue<Runnable> linkedBlockingQueue = new LinkedBlockingQueue<>(100);
       ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,3,1, TimeUnit.MINUTES,linkedBlockingQueue, Executors.defaultThreadFactory(),rejectHandler);


    }

    private static void runnableTask() throws ExecutionException, InterruptedException {
        // runnable形式创建线程
/*        RunnableTask runnableTask = new RunnableTask();
        new Thread(runnableTask).start();*/

        // thread形式创建线程
/*        ThreadTask threadTask = new ThreadTask();
        threadTask.start();*/

        // Callable形式创建线程
/*        CallableTask callableTask = new CallableTask();
        FutureTask<String> futureTask = new FutureTask<>(callableTask);
        new Thread(futureTask).start();
        // futureTask.isDone()  FutureTask在初始化完后  初始状态state = NEW
        // 此是线程异步执行 当执行完后 state != NEW
        // 注意: 请勿在主线程中防止过早的使用futureTask.isDone()
        // 或者利用!futureTask.isDone()阻塞主线程 等跳出循环时  就是任务执行完成时
        while (!futureTask.isDone()){
            System.out.println("异步线程正在执行中");
        }
        String res = futureTask.get();
        System.out.println("异步线程执行完成,执行完成后的结果为: "+res);*/
    }

    static class RunnableTask implements Runnable {
        @Override
        public void run() {
            System.out.println("RunnableTask---------");
        }
    }

    static class ThreadTask extends Thread {
        @Override
        public void run() {
            System.out.println("ThreadTask---------");
        }
    }

    static class CallableTask implements Callable<String> {
        @Override
        public String call() throws Exception {
            Thread.sleep(2000);
            System.out.println("CallableTask---------");
            return "CallableTask";
        }
    }

}
